package com.ruoyi.system.service.impl.trade;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.common.VehicleStateConstant;
import com.ruoyi.system.domain.db.basic.BasicVehicles;
import com.ruoyi.system.domain.db.trade.TradeVehicleOrder;
import com.ruoyi.system.mapper.basic.BasicMaterialsMapper;
import com.ruoyi.system.mapper.basic.BasicVehiclesMapper;
import com.ruoyi.system.mapper.trade.TradeVehicleOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.ruoyi.system.service.ITradeVehicleOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 汽车采购Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
public class TradeVehicleOrderServiceImpl implements ITradeVehicleOrderService 
{
    @Resource
    private TradeVehicleOrderMapper tradeVehicleOrderMapper;

    @Resource
    private BasicVehiclesMapper basicVehiclesMapper;

    /**
     * 查询汽车采购
     * 
     * @param orderId 汽车采购主键
     * @return 汽车采购
     */
    @Override
    public TradeVehicleOrder selectTradeVehicleOrderByOrderId(Long orderId)
    {
        return tradeVehicleOrderMapper.selectTradeVehicleOrderByOrderId(orderId);
    }

    /**
     * 查询汽车采购列表
     * 
     * @param tradeVehicleOrder 汽车采购
     * @return 汽车采购
     */
    @Override
    public List<TradeVehicleOrder> selectTradeVehicleOrderList(TradeVehicleOrder tradeVehicleOrder)
    {

        return tradeVehicleOrderMapper.selectTradeVehicleOrderList(tradeVehicleOrder);
    }

    /**
     * 新增汽车采购
     * 
     * @param tradeVehicleOrder 汽车采购
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTradeVehicleOrder(TradeVehicleOrder tradeVehicleOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        tradeVehicleOrder.setCreateTime(nowDate);
        tradeVehicleOrder.setUpdateTime(nowDate);
        tradeVehicleOrder.setCreateBy(loginUser.getUserId());
        tradeVehicleOrder.setUpdateBy(loginUser.getUserId());
        tradeVehicleOrder.setTotal(tradeVehicleOrder.getPrice()*tradeVehicleOrder.getQuantity());

        // 汽车表中增加记录
        for (int i = 0; i < tradeVehicleOrder.getQuantity(); i++) {
            BasicVehicles basicVehicles = new BasicVehicles();
            Date nowDate1 = DateUtils.getNowDate();
            basicVehicles.setVehicleId(null);
            basicVehicles.setVehicleNumber(null);
            basicVehicles.setManufacturer(tradeVehicleOrder.getManufacturer());
            basicVehicles.setModel(tradeVehicleOrder.getModel());
            basicVehicles.setCapacity(tradeVehicleOrder.getCapacity());
            basicVehicles.setState(VehicleStateConstant.VEHICLE_STATE_UNAVAILABLE);
            basicVehicles.setRegistrationDate(nowDate1);
            basicVehicles.setLastMaintenanceDate(nowDate1);
            basicVehicles.setDriverId(null);
            basicVehicles.setIsDelete(0);
            basicVehicles.setCreateBy(loginUser.getUserId());
            basicVehicles.setCreateTime(nowDate1);
            basicVehicles.setUpdateBy(loginUser.getUserId());
            basicVehicles.setUpdateTime(nowDate1);
            basicVehiclesMapper.insertBasicVehicles(basicVehicles);
        }

        return tradeVehicleOrderMapper.insertTradeVehicleOrder(tradeVehicleOrder);
    }

    /**
     * 修改汽车采购
     * 
     * @param tradeVehicleOrder 汽车采购
     * @return 结果
     */
    @Override
    public int updateTradeVehicleOrder(TradeVehicleOrder tradeVehicleOrder)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        tradeVehicleOrder.setTotal(tradeVehicleOrder.getPrice()*tradeVehicleOrder.getQuantity());
        tradeVehicleOrder.setUpdateTime(nowDate);
        tradeVehicleOrder.setUpdateBy(loginUser.getUserId());
        return tradeVehicleOrderMapper.updateTradeVehicleOrder(tradeVehicleOrder);
    }

    /**
     * 批量删除汽车采购
     * 
     * @param orderIds 需要删除的汽车采购主键
     * @return 结果
     */
    @Override
    public int deleteTradeVehicleOrderByOrderIds(Long[] orderIds)
    {
        return tradeVehicleOrderMapper.deleteTradeVehicleOrderByOrderIds(orderIds);
    }

    /**
     * 删除汽车采购信息
     * 
     * @param orderId 汽车采购主键
     * @return 结果
     */
    @Override
    public int deleteTradeVehicleOrderByOrderId(Long orderId)
    {
        return tradeVehicleOrderMapper.deleteTradeVehicleOrderByOrderId(orderId);
    }
}
